// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database();
const _ = db.command;
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext();
  let id = event.index_id;
  let keys = event.keys;
  let openId = wxContext.openId;
  if(event.code == 1){
    let res = await db.collection('list_1').where({
      recipeName: db.RegExp({
        regexp:keys,
        options: 'i',
      })
    }).get();
    return res;
  }
  if(event.code == 2){
    return await db.collection('list_1').orderBy('follows','desc').limit(5).get();
  }
  await db.collection('list_1').where({
    _id:id
  }).update({
    data:{
      views:_.inc(1)
    }
  })
  let res = await db.collection('list_1').where({
    _id:id
  }).get();
  return {
   res
  }
}